package com.clss.emergencycall.view;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clss.emergencycall.R;
import com.clss.emergencycall.utils.Lg;

import java.util.Objects;

/**
 * Created by 唐浩 on 2018/4/11.
 */

public class NormalFragmentDialog extends DialogFragment implements View.OnClickListener {
    private static final String TAG = "NormalFragmentDialog";
    private static final String NO_TITLE = "no_title";

    private String mContent = "";//内容
    private SpannableStringBuilder mSBContent;
    private OkView mOkView;
    private Button mBtnOk;
    private TextView mLocTv;
    private String mLocal;
    private View.OnClickListener mLocalClickListener = null;

    private SpannableStringBuilder mSbStrOk = null;
    private String mStrOk="确定";
    private String mStrNo="取消";

    private boolean isShowBtnLeft = true;

    public static NormalFragmentDialog getInstance(){
        return new NormalFragmentDialog();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //1 通过样式定义,DialogFragment.STYLE_NORMAL这个很关键的
//        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog1);

        //2代码设置 无标题 无边框  这个就很坑爹，这么设置很多系统效果就都没有了
        //setStyle(DialogFragment.STYLE_NO_TITLE|DialogFragment.STYLE_NO_FRAME,0);
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        /***
         *   在此处设置 无标题 对话框背景色
         //getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
         // dialog的背景色
         //getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
         //getDialog().getWindow().setDimAmount(0.8f);//背景黑暗度
         */
        Objects.requireNonNull(getDialog().getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return inflater.inflate(R.layout.dialog_custom_alarm, container,false);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        mBtnOk = view.findViewById(R.id.btn_dialog_positive);
        Button cancel = view.findViewById(R.id.btn_dialog_negative);
        LinearLayout mLlBack = view.findViewById(R.id.ll_layout_back);
        mLocTv = view.findViewById(R.id.dialog_local_tv);
        Lg.i(TAG, "---mLocTv == null===" + (mLocTv == null));
        if (TextUtils.isEmpty(mLocal)){
            mLocTv.setVisibility(View.GONE);
        }else {
            mLocTv.setOnClickListener(mLocalClickListener);
            setLocalContent();
        }
        mBtnOk.setOnClickListener(this);
        cancel.setOnClickListener(this);
        mBtnOk.setVisibility((isShowBtnLeft)?View.VISIBLE:View.GONE);
        if (mSbStrOk == null){
            mBtnOk.setText(mStrOk);
        }else {
            mBtnOk.setText(mSbStrOk);
            if ("拨打110".equals(mStrOk) ){
                mBtnOk.setTextColor(Color.RED);
            }
        }
//        mBtnOk.setText(Objects.equals(mSbStrOk, null) ?mStrOk:mSbStrOk);
        cancel.setText(mStrNo);
        TextView mTvContent = view.findViewById(R.id.tv_dialog_message);
        mTvContent.setText(Objects.equals(mSBContent, null) ? mContent : mSBContent);
        mLlBack.setBackgroundResource(R.drawable.dialog_background);
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_dialog_negative:
                this.dismiss();
                break;
            case R.id.btn_dialog_positive:
                if (mOkView != null) {
                    mOkView.okLisener();
                }
                dismiss();
                break;
            default:
                break;
        }
    }

    public NormalFragmentDialog getTvContent(String s) {
        mContent = s;
        return this;
    }

    public NormalFragmentDialog getTvContent(String s, int color, int startIndex, int overIndex) {
        SpannableStringBuilder builder = new SpannableStringBuilder(s);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        builder.setSpan(redSpan, startIndex, overIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mSBContent = builder;
        return this;
    }

    public NormalFragmentDialog setStrOk(String strOk, int color) {
        SpannableStringBuilder builder = new SpannableStringBuilder(strOk);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        builder.setSpan(redSpan, 0, strOk.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mSbStrOk = builder;
        mStrOk = strOk;
        return this;
    }

    public NormalFragmentDialog setLeftButtonVisible(boolean b){
        isShowBtnLeft = b;
        return this;
    }

    public NormalFragmentDialog setokListener(OkView view) {
        this.mOkView = view;
        return this;
    }

    public interface OkView{
        void okLisener();
    }


    public NormalFragmentDialog setStrOk(String strOk) {
        mStrOk = strOk;
        return this;
    }

    public NormalFragmentDialog setStrCancel(String StrCancel) {
        mStrNo = StrCancel;
        return this;
    }

    public NormalFragmentDialog setLocalTv(String local, View.OnClickListener listener){
        mLocal = local;
        mLocalClickListener = listener;
        setLocalContent();
        return this;
    }

    private void setLocalContent(){
        if (mLocTv != null){
            mLocTv.setVisibility(View.VISIBLE);
            mLocTv.setText(mLocal + "(可选择)");
        }
    }
}
